package org.springframework.samples.jpetstore.domain;

import java.io.Serializable;

public class CartItem implements Serializable {

  /* Private Fields */

  private static final long serialVersionUID = -9199478855687368455L;
  private Item item;
  private int quantity;
  private boolean inStock;

  /**
   * Instantiate a CartItem in a valid state
   * @param item The Item in the Cart
   * @param quantity The number of items
   * @param isInStock Is the Item in stock
   */
  public CartItem(Item item, int quantity, boolean isInStock) {
	 if (item==null) throw new IllegalArgumentException("CartItem: Item==null");
     this.setItem(item);
     this.setQuantity(0);
     this.setInStock(isInStock);
  }  
  
  public CartItem() {
	  
  }
  
  /* JavaBeans Properties */
 
  public boolean isInStock() { return inStock; }
  public void setInStock(boolean inStock) { this.inStock = inStock; }

  public Item getItem() { return item; }
  public void setItem(Item item) {
    this.item = item;
  }

  public int getQuantity() { return quantity; }
  public void setQuantity(int quantity) {
    this.quantity = quantity;
  }

	public double getTotalPrice() {
		if (item != null) {
			return item.getListPrice() * quantity;
		}
		else {
			return 0;
		}
	}

  /* Public methods */

  public void incrementQuantity() {
    quantity++;
  }

}
